﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using dokuzeylulworkshop.BaseUtility;
using DokuzEylul.Sinav.Entities;
using DokuzEylul.Sinav.Utility_Master;
using System.Collections.Generic;

namespace DokuzEylul.Sinav.YonetimPaneli
{
    public partial class Kullanici : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataBind();
                this.IsSave = true;
            }
        }
        public bool IsSave
        {
            get
            {
                return (bool)ViewState["IsSave"];
            }
            set
            {
                ViewState["IsSave"] = value;
            }
        }
        public IList<SinavUser> CurrentUserList
        {
            get
            {
                IList<SinavUser> _result = Session["Current_User_List"] as IList<SinavUser>;
                if (_result == null)
                    Session["Current_User_List"] = _result = m_dbmaster.UserInstance.GetSinavUsers();
                return _result;
            }
            set
            {
                Session["Current_User_List"] = value;
            }
        }
        public SinavUser CurrentUser
        {
            get
            {
                SinavUser _result = Session["Current_SinavUser"] as SinavUser;
                if (_result == null)
                    Session["Current_SinavUser"] = _result = new SinavUser();
                return _result;
            }
            set
            {
                Session["Current_SinavUser"] = value;
            }
        }
        public override int m_MenuID
        {
            get
            {
                return 10102;
            }
        }
        protected void btnYeni_Click(object sender, EventArgs e)
        {
            pnlcontent.Enabled = true;
            this.IsSave = true;
            this.clearform();
        }
        protected void btnKaydet_Click(object sender, EventArgs e)
        {
            if (txtPassWord.Text != txtPassWordAgain.Text)
            {
                ltlinfo.Text = "farklı şifreler girdiniz!";
                return;
            }
            this.CurrentUser.Email = txtMailAdres.Text;
            this.CurrentUser.Password = txtPassWord.Text;
            this.CurrentUser.UserName = txtUserName.Text;
            if (IsSave)
            {
                this.CurrentUser.UserId = Guid.NewGuid();
                this.CurrentUserList.Add(CurrentUser);
                m_dbmaster.UserInstance.SaveUser(CurrentUser);
                //TODO - aynı kullanıcı ismi gelmesi kontrol edilmeli database unique key mevcut o kullanılacak
                this.clearform();
            }
            else
            {
                m_dbmaster.UserInstance.UpdateUser(CurrentUser);
                for (int i = 0; i < CurrentUserList.Count; i++)
                {
                    if (CurrentUser.UserId == CurrentUserList[i].UserId)
                    {
                        CurrentUserList[i].UserName = CurrentUser.UserName;
                        CurrentUserList[i].Password  = CurrentUser.Password;
                        CurrentUserList[i].Email = CurrentUser.Email;
                    }
                }
            }
            GridView1.DataBind();
        }
        private void clearform()
        {
            foreach (Control _crtl in pnlcontent.Controls)
            {
                if (_crtl is TextBox)
                    (_crtl as TextBox).Text = "";
            }
        }
        protected void btnSil_Click(object sender, EventArgs e)
        {
            CurrentUserList.Remove(CurrentUser);
            m_dbmaster.UserInstance.DeleteUser(CurrentUser);
            GridView1.DataBind();
            this.clearform();
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            DataKey _key = GridView1.SelectedDataKey;
            
            this.CurrentUser = m_dbmaster.UserInstance.GetUser((Guid)_key.Value);
            txtMailAdres.Text = this.CurrentUser.Email;
            txtPassWord.Text = this.CurrentUser.Password;
            txtPassWordAgain.Text = this.CurrentUser.Password;
            txtUserName.Text = this.CurrentUser.UserName;

            pnlcontent.Enabled = true;
            this.IsSave = false;
        }
        protected void GridView1_DataBinding(object sender, EventArgs e)
        {
            GridView1.DataSource = this.CurrentUserList;
        }
    }
}
